package com.frxuexi.myroom.view

import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.RoutePath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.User
import com.frxuexi.myroom.intent.UserIntent
import com.frxuexi.myroom.viewmodel.UserViewModel
import com.tencent.mmkv.MMKV

@Composable
fun LoginView(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    var name by remember {
        mutableStateOf("")
    }
    var pass by remember {
        mutableStateOf("")
    }
    var role by remember {
        mutableStateOf("员工")
    }
    LaunchedEffect(key1 = "", block = {
        vm.state.collect {
            when (it) {
                is UiState.onSuccess<*> -> {
                    Log.i("===", "登录成功"+it.data)
                    val user = it.data as User
                    MMKV.defaultMMKV().encode("name",name)
                    MMKV.defaultMMKV().encode("uid",user.uid!!)
                    MMKV.defaultMMKV().encode("role",role)
                    navHostController.popBackStack()
                }

                else -> {}
            }
        }

    })

    Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(20.dp)) {
        val modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 20.dp)


        BaseTitle(title = "登录", navHostController = navHostController)
        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(
            value = name,
            modifier = modifier,
            onValueChange = { name = it },
            label = { Text(text = "请输入用户名") })
        OutlinedTextField(
            value = pass,
            modifier = modifier,
            onValueChange = { pass = it },
            label = { Text(text = "请输入密码") },
            visualTransformation = PasswordVisualTransformation()
        )
        Row(modifier = Modifier.padding(start = 20.dp),horizontalArrangement = Arrangement.spacedBy(10.dp), verticalAlignment = Alignment.CenterVertically) {
            Text(text = "职位：")
            Checkbox(checked = role=="员工", onCheckedChange = {
                if(it){
                    role="员工"
                }
            })
            Text(text = "员工")
            Checkbox(checked = role=="经理", onCheckedChange = {
                if(it){
                    role="经理"
                }
            })
            Text(text = "经理")

        }


        Button(modifier = modifier, onClick = {
            if(name.isNullOrEmpty() || pass.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@Button
            }
            if(!name.matches(Regex("[A-z0-9_]{3,10}"))){
                ToastUtils.showLong("不是合法用户名")
                return@Button
            }
            vm.sendIntent(UserIntent.login(name,pass,role))
        }) {
            Text(text = "登录")
        }
        Text(text = "没有账号？去注册",
            Modifier
                .padding(start = 20.dp)
                .clickable {
//                    navHostController.navigate(RoutePath.REGISTER)
                    navHostController.navigate(RoutePath.ROOMREGISTER)
                })
    }
}